//
//  GXShopDetailHeaderView.m
//  YIChuSeller
//
//  Created by 田小椿 on 2018/12/5.
//  Copyright © 2018年 xiangshengjun. All rights reserved.
//
#import "GXShopDetailHeaderView.h"
#import "SDCycleScrollView.h"
#import "UIButton+IconPosition.h"
@interface GXShopDetailHeaderView()<SDCycleScrollViewDelegate>
@property (nonatomic, weak)SDCycleScrollView *bannerView;
@property (nonatomic,strong)UIView *totalView;
@property (nonatomic,strong)UILabel * nameLabel;
@property (nonatomic,strong)UIView * oneLineView;
@property (nonatomic,strong)UIView * twoLineView;
@property (nonatomic,strong)UIImageView * timeImageView;
@property (nonatomic,strong)UIImageView * locationImageView;
@property (nonatomic,strong)UILabel * timeLabel;
@property (nonatomic,strong)UILabel * locationLabel;
@property (nonatomic,strong) NSDictionary * titleKeyDic;
@property (nonatomic,strong) NSDictionary * imageKeyDic;
@end
@implementation GXShopDetailHeaderView

-(instancetype)initWithFrame:(CGRect)frame{
    self=[super initWithFrame:frame];
    if (self) {
        [self setBackgroundColor:[UIColor groupTableViewBackgroundColor] ];
        [self setupBannerView];
        [self setupViews];
    }
    return self;
}

- (void)setupViews
{
    [self addSubview:self.totalView];
    [self.totalView addSubview:self.nameLabel];
    [self.totalView addSubview:self.oneLineView];
    [self.totalView addSubview:self.timeImageView];
    [self.totalView addSubview:self.timeLabel];
    [self.totalView addSubview:self.twoLineView];
    [self.totalView addSubview:self.locationImageView];
    [self.totalView addSubview:self.locationLabel];
}

- (void)setupBannerView
{
    [self addSubview:self.bannerView];
    UIButton * backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backButton setImage:[UIImage imageNamed:@"common_leftItem"] forState:UIControlStateNormal];
    backButton.frame = CGRectMake(10, Height_StatusBar+10, 20, 25);
    [backButton addTarget:self action:@selector(pressBackBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.bannerView addSubview:backButton];
    
}
- (SDCycleScrollView *)bannerView
{
    if (_bannerView == nil) {
        _bannerView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, kUIScreenWidth, 220) imageURLStringsGroup:nil];
        _bannerView.backgroundColor = RGBColor(229, 229, 229);
        _bannerView.delegate = self;
        _bannerView.pageControlAliment=SDCycleScrollViewPageContolAlimentCenter;
        _bannerView.titleLabelTextFont=[UIFont systemFontOfSize:12];
        _bannerView.pageControlStyle = SDCycleScrollViewPageContolStyleClassic;
        _bannerView.placeholderImage = [UIImage imageNamed:@"home_placeHolder"];
    }
    return _bannerView;
}


- (void)pressBackBtn:(UIButton *)btn
{
    [self.getViewController.navigationController popViewControllerAnimated:YES];
}
- (void)setBannerItems:(NSArray *)bannerItems
{
    _bannerItems = bannerItems;
    NSMutableArray *bannerArr=[NSMutableArray array];
    for (NSDictionary  *bannerDic in bannerItems) {
        NSString *urlStr = [NSString stringWithFormat:@"%@/%@",kBaseURLIMAGE,bannerDic[@"dishesImage"]];
        [bannerArr addObject:urlStr];
    }
    self.bannerView.imageURLStringsGroup=bannerArr;
}


- (UIView *)totalView
{
    if (_totalView == nil) {
        _totalView = [[UIView alloc]init];
        _totalView.backgroundColor = [UIColor whiteColor];
        _totalView.layer.masksToBounds = YES;
        [_totalView.layer setCornerRadius:3];
    }
    return _totalView;
}

- (UILabel *)nameLabel
{
    if (_nameLabel == nil) {
        _nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 100, 17)];
        _nameLabel.text = @"店铺服务";
        _nameLabel.font = [UIFont systemFontOfSize:14];
    }
    return _nameLabel;
}

- (UIView *)oneLineView
{
    if (_oneLineView == nil) {
        _oneLineView  =[[UIView alloc]init];
        _oneLineView .backgroundColor = RGBColor(241, 237, 238);
    }
    return _oneLineView;
}
- (UIImageView *)timeImageView
{
    if (_timeImageView == nil) {
        _timeImageView = [[UIImageView alloc]init];
        _timeImageView.image = [UIImage imageNamed:@"store_opendatetime"];
    }
    return _timeImageView;
}
- (UILabel *)timeLabel
{
    if (_timeLabel == nil) {
        _timeLabel = [[UILabel alloc]init];
        _timeLabel.text = @"周一至周日 08:00-23:00";
        _timeLabel.textColor = [UIColor lightGrayColor];
        _timeLabel.font = [UIFont systemFontOfSize:13];
    }
    return _timeLabel;
}
- (UIView *)twoLineView
{
    if (_twoLineView == nil) {
        _twoLineView =[[UIView alloc]init];
        _twoLineView.backgroundColor = RGBColor(241, 237, 238);
    }
    return _twoLineView;
}
- (UIImageView *)locationImageView
{
    if (_locationImageView == nil) {
        _locationImageView = [[UIImageView alloc]init];
        _locationImageView.image = [UIImage imageNamed:@"store_address"];
    }
    return _locationImageView;
}
- (UILabel *)locationLabel
{
    if (_locationLabel == nil) {
        _locationLabel = [[UILabel alloc]init];
        _locationLabel.text = @"雨花区芙蓉中路与劳动西路东北角";
        _locationLabel.textColor = [UIColor lightGrayColor];
        _locationLabel.font = [UIFont systemFontOfSize:13];
    }
    return _locationLabel;
}

- (void)setContentDict:(NSDictionary *)contentDict
{
    _contentDict = contentDict;
    
    NSArray * serviceArray = [contentDict[@"serviceSige"]  componentsSeparatedByString:@"-"];
    if (!serviceArray.count) {
        return;
    }
    [self handleServiceArray:serviceArray];
    NSInteger viewYFrame = serviceArray.count / 5 ;
    NSInteger index = serviceArray.count % 5;
    NSInteger viewY = index ?(viewYFrame + 1) * 45 : viewYFrame*45;
    self.oneLineView.frame = CGRectMake(5, viewY +40, kUIScreenWidth-20, 1);
    self.timeImageView.frame = CGRectMake(10,viewY+51, 20 , 20);
    self.timeLabel.frame =CGRectMake(35, viewY+51, kUIScreenWidth-50, 20);
    self.twoLineView.frame =CGRectMake(5, viewY +81, kUIScreenWidth-20, 1);
    self.locationImageView.frame =CGRectMake(10,viewY+92, 20 , 20);
    self.locationLabel.frame =CGRectMake(35, viewY+92, kUIScreenWidth-50, 20);
    self.totalView.frame =CGRectMake(10, 235, kUIScreenWidth-20, viewY+122);
    self.headerViewHeight = viewY+357;
    NSString *dayStr = [NSString getDayString:self.contentDict[@"businesSige"]];
    self.timeLabel.text = [NSString stringWithFormat:@"%@ %@",dayStr,self.contentDict[@"businesstime"]];
    self.locationLabel.text = self.contentDict[@"address"];
}

- (void)handleServiceArray:(NSArray *)serviceArray
{
    CGFloat Start_X = 10;
    CGFloat Start_Y = 40;
    CGFloat Width_Space = 0;
    CGFloat Height_Space  = 0;
    CGFloat btn_heigth = 45;
    CGFloat btn_width = (kUIScreenWidth-40)/5;
    for (int i= 0; i<serviceArray.count;i++) {
        NSInteger index = i % 5;
        NSInteger page = i / 5;
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        NSString * imageStr = [self.imageKeyDic objectForKey:serviceArray[i]];
        [button setImage:[UIImage imageNamed:imageStr] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:12];
        [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        [button setBackgroundColor:[UIColor clearColor]];
        NSString *titleStr =[self.titleKeyDic objectForKey:serviceArray[i]];
        [button setTitle:titleStr forState:UIControlStateNormal];
        button.frame = CGRectMake(index * (btn_width + Width_Space) + Start_X, page  * (btn_heigth + Height_Space)+Start_Y, btn_width, btn_heigth);
        [button setIconInTopWithSpacing:3];
        [self.totalView addSubview:button];
    }
}

#pragma mark - SDCycleScrollViewDelegate
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{

}

- (NSDictionary *)titleKeyDic
{
    if (_titleKeyDic ==nil) {
        _titleKeyDic = [NSDictionary dictionary];
        _titleKeyDic= @{
                        @"A":@"露天座",
                        @"B":@"包间",
                        @"C":@"WIFI",
                        @"D":@"宝宝椅",
                        @"E":@"停车",
                        @"F":@"防油服",
                        @"G":@"游乐区",
                        @"H":@"刷卡",
                        @"I":@"无烟区",
                        @"J":@"可吸烟",
                        @"K":@"沙发位",
                        };
    }
    return _titleKeyDic;
}
- (NSDictionary *)imageKeyDic
{
    if (_imageKeyDic == nil) {
        _imageKeyDic = [NSDictionary dictionary];
        _imageKeyDic = @{
                         @"A":@"home_service11normal",
                         @"B":@"home_service12normal",
                         @"C":@"home_service13normal",
                         @"D":@"home_service14normal",
                         @"E":@"home_service15normal",
                         @"F":@"home_service21normal",
                         @"G":@"home_service22normal",
                         @"H":@"home_service23normal",
                         @"I":@"home_service24normal",
                         @"J":@"home_service25normal",
                         @"K":@"home_service31normal",
                         };
    }
    return _imageKeyDic;
}



@end
